package com.oreilly.ez.fileserver.service.impl;


import com.oreilly.ez.fileserver.entity.NoteArticle;
import com.oreilly.ez.fileserver.entity.NoteArticleVO;
import com.oreilly.ez.fileserver.repo.NoteArticleRepo;
import com.oreilly.ez.fileserver.repo.NoteArticleVORepo;
import com.oreilly.ez.fileserver.service.INoteArticleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class NoteArticleService implements INoteArticleService {

   @Autowired
   private NoteArticleRepo repo;

   @Autowired
   private NoteArticleVORepo voRepo;

    @Override
    public void add(NoteArticle item) {
        repo.save(item);
    }

    @Override
    public void deleteByIds(String[] ids) {

        for (String id : ids) {
            repo.deleteById(id);
        }
    }

    @Override
    public void update(NoteArticle NoteArticle) {

        repo.save(NoteArticle);
    }

    @Override
    public Page<NoteArticle> find(NoteArticle item, Pageable pageable) {
        Specification<NoteArticle> specification = (root, query, criteriaBuilder) -> {

            List<Predicate> list = new ArrayList<>();
            String cateId = item.getCateId();

            if(StringUtils.isNotBlank(cateId)){

                list.add(criteriaBuilder.equal(root.get("cateId").as(String.class), cateId));
            }

            String title = item.getTitle();
            if(StringUtils.isNotBlank(title)){
                list.add(criteriaBuilder.like(root.get("title").as(String.class), "%" + title + "%"));
            }

            Predicate[] p = new Predicate[list.size()];
            return criteriaBuilder.and(list.toArray(p));
        };
//
        return repo.findAll(specification, pageable);

//        return repo.findByTitleLike(specification, pageable);
    }

    @Override
    public Page<NoteArticleVO> findVO(NoteArticle item, Pageable pageable) {
        Specification<NoteArticleVO> specification = (root, query, criteriaBuilder) -> {

            List<Predicate> list = new ArrayList<>();
            String cateId = item.getCateId();

            if(StringUtils.isNotBlank(cateId)){

                list.add(criteriaBuilder.equal(root.get("cateId").as(String.class), cateId));
            }

            String title = item.getTitle();
            if(StringUtils.isNotBlank(title)){
                list.add(criteriaBuilder.like(root.get("title").as(String.class), "%" + title + "%"));
            }

            Predicate[] p = new Predicate[list.size()];
            return criteriaBuilder.and(list.toArray(p));
        };



//
        return voRepo.findAll(specification, pageable);
    }

    @Override
    public NoteArticle findById(String id) {
        return repo.findById(id).get();
    }


}
